
package com.amigo.online.provider.manager.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.manager.mapper.AdvTanApplyTblMapper;
import com.amigo.online.provider.manager.mapper.AdvTanAreaTblMapper;
import com.amigo.online.provider.manager.mapper.AdvTanStockTblMapper;
import com.amigo.online.provider.manager.model.entity.AdvAreaTbl;
import com.amigo.online.provider.manager.model.entity.AdvNearbyApplyTbl;
import com.amigo.online.provider.manager.model.entity.AdvTanApplyTbl;
import com.amigo.online.provider.manager.model.entity.AdvTanAreaTbl;
import com.amigo.online.provider.manager.model.entity.AdvTanStockTbl;
import com.amigo.online.provider.manager.result.AdvTanInfo;
import com.amigo.online.provider.manager.service.AdvTanApplyTblService;
import com.amigo.online.provider.manager.util.OrderUtils;

/**
 * <code>AdvTanApplyTblServiceImpl.java</code>
 * <p>
 * : 探探广告信息实现
 * </p>
 * <p>
 *
 * @author LuChen
 * @version 1.0
 * @company Amigo
 * @time 2018年11月29日 上午10:59:27
 * @lastUpdate 无
 * </p>
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AdvTanApplyTblServiceImpl implements AdvTanApplyTblService {


    @Autowired
    private AdvTanApplyTblMapper advTanApplyTblMapper;

    @Autowired
    private AdvTanStockTblMapper advTanStockTblMapper;

    @Autowired
    private AdvTanAreaTblMapper advTanAreaTblMapper;


    @Override
    public AdvTanApplyTbl addAdvTanApply(AdvTanApplyTbl advTanApplyTbl) {
        String orderId = OrderUtils.createOrderNumber();
        advTanApplyTbl.setOrderId(orderId);
        Long nowTime = System.currentTimeMillis() / 1000;
        advTanApplyTbl.setCreatedTime(nowTime);
        advTanApplyTbl.setStatus(0);
        advTanApplyTbl.setDeleted(0);
        if (advTanApplyTblMapper.insert(advTanApplyTbl) > 0) {
            List<Long> areaId = advTanApplyTbl.getAreaId();
            AdvTanApplyTbl selectOne = advTanApplyTblMapper.selectOne(advTanApplyTbl);
            Long advId = selectOne.getId();
            if (areaId != null && areaId.size() > 0) {
                List<AdvTanAreaTbl> list = new ArrayList<>();
                for (Long area : areaId) {
                    AdvTanAreaTbl e = new AdvTanAreaTbl();
                    e.setAdvId(advId);
                    e.setAreaId(area);
                    list.add(e);
                }
                if (advTanAreaTblMapper.insertList(list) > 0) {
                    return selectOne;
                }
            } else {
                return null;
            }
        }
        return null;
    }

    @Override
    public boolean updateAdvTanApply(AdvTanApplyTbl advTanApplyTbl) {
        AdvTanApplyTbl selectById = advTanApplyTblMapper.selectById(advTanApplyTbl.getId());
        if (selectById.getStatus() == null || selectById.getStatus().equals(1)) {
            return false;
        }
        if (advTanApplyTblMapper.updateById(advTanApplyTbl) > 0) {
            List<Long> areaId = advTanApplyTbl.getAreaId();
            if (areaId != null && areaId.size() > 0) {
                Long advId = advTanApplyTbl.getId();
                AdvTanAreaTbl ex = new AdvTanAreaTbl();
                ex.setAdvId(advId);
                advTanAreaTblMapper.delete(ex);
                List<AdvTanAreaTbl> list = new ArrayList<>();
                for (Long area : areaId) {
                    AdvTanAreaTbl e = new AdvTanAreaTbl();
                    e.setAdvId(advId);
                    e.setAreaId(area);
                    list.add(e);
                }
                if (advTanAreaTblMapper.insertList(list) > 0) {
                    return true;
                }
            }
        }
        return false;
    }


    @Override
    public boolean deleteById(AdvTanApplyTbl advTanApplyTbl) {
        Long deleteTime = System.currentTimeMillis() / 1000;
        advTanApplyTbl.setDeletedTime(deleteTime);
        if (advTanApplyTblMapper.deletebyId(advTanApplyTbl) > 0) {
            return true;
        }
        return false;
    }


    @Override
    public JSONObject findTanAdv(AdvTanApplyTbl advTanApplyTbl) {
        if (advTanApplyTbl.getPageNo() == null || advTanApplyTbl.getPageNo() < 1) {
            advTanApplyTbl.setPageNo(1);
        }
        if (advTanApplyTbl.getPageCount() == null || advTanApplyTbl.getPageCount() < 1) {
            advTanApplyTbl.setPageCount(10);
        }
        int totalCount = advTanApplyTblMapper.findTanAdvCount(advTanApplyTbl);
        int totalPage = totalCount % advTanApplyTbl.getPageCount() == 0 ? totalCount / advTanApplyTbl.getPageCount() : totalCount / advTanApplyTbl.getPageCount() + 1;
        int pageStart = (advTanApplyTbl.getPageNo() - 1) * advTanApplyTbl.getPageCount();
        advTanApplyTbl.setPageStart(pageStart);
        List<AdvNearbyApplyTbl> list = advTanApplyTblMapper.findTanAdv(advTanApplyTbl);
        JSONObject re = new JSONObject();
        re.put("list", list);
        re.put("totalCount", totalCount);
        re.put("totalPage", totalPage);
        re.put("pageNo", advTanApplyTbl.getPageNo());
        re.put("pageCount", advTanApplyTbl.getPageCount());
        return re;
    }

    @Override
    public AdvTanInfo findTanAdvDetailInfo(Long id) {
        AdvTanInfo info = advTanApplyTblMapper.findTanAdvDetailInfo(id);
        List<AdvAreaTbl> areaInfo = advTanApplyTblMapper.findTanAdvDetailAreaInfo(id);
        info.setAreaList(areaInfo);
        return info;
    }


    @Override
    public boolean checkAdv(AdvTanApplyTbl advTanApplyTbl) {
        Long id = advTanApplyTbl.getId();
        advTanApplyTblMapper.updateById(advTanApplyTbl);
        if (advTanApplyTbl.getStatus() == 1) {
            AdvTanApplyTbl adv = advTanApplyTblMapper.selectById(id);
            AdvTanStockTbl advStock = new AdvTanStockTbl();
            advStock.setAdvId(adv.getId());
            advStock.setApplyNumber(adv.getTotalNumber());
            advStock.setRemainingNumber(adv.getTotalNumber());
            advStock.setOverUsedNumber(0);
            advStock.setUsedNumber(0);
            if (advTanStockTblMapper.insert(advStock) > 0) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

}

